<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Guava函数式编程深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9f9f9;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            padding: 1.5rem;
            position: relative;
            overflow: hidden;
        }
        .code-block:before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(to bottom, #ff8a00, #e52e71);
        }
        .highlight-box {
            background: linear-gradient(to right, #f9f9f9, #ffffff);
            border-left: 4px solid #6e8efb;
            padding: 1.5rem;
            margin: 1.5rem 0;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .section-divider {
            width: 80px;
            height: 4px;
            background: linear-gradient(to right, #6e8efb, #a777e3);
            margin: 2rem auto;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        .floating-badge {
            position: absolute;
            top: -12px;
            right: 20px;
            background: #6e8efb;
            color: white;
            padding: 0.25rem 1rem;
            border-radius: 20px;
            font-size: 0.75rem;
            font-weight: 600;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Guava函数式编程深度解析</h1>
            <div class="text-xl md:text-2xl font-light mb-8 max-w-3xl mx-auto">
                探索Java 8之前函数式编程的最佳实践与实现原理
            </div>
            <div class="bg-white bg-opacity-20 rounded-lg p-6 inline-block max-w-2xl mx-auto">
                <p class="text-lg italic">
                    "Guava的函数式编程工具包虽然在Java 8后光芒减弱，但其设计思想和实现技巧依然值得我们学习。"
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8 mb-8">
                <p class="text-lg text-gray-700 leading-relaxed">
                    在Java 8之前，函数式编程在Java中一直是个痛点。记得我曾经为了在Java 6项目中实现一个简单的map操作，写了一堆匿名内部类，代码臃肿得令人发指。直到接触了Guava的函数式工具，才让我看到了曙光。
                </p>
            </div>
        </section>

        <!-- Section 1 -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="bg-blue-100 text-blue-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">1</span>
                函数式接口设计与实现原理
            </h2>
            <p class="text-gray-600 mb-6">
                在Java 8之前，Guava通过一系列精心设计的函数式接口，将函数式编程的思想引入Java。这些接口构成了Guava函数式编程的基础。
            </p>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-code text-blue-500 mr-3"></i>
                            核心函数式接口
                        </h3>
                        <div class="code-block">
                            <pre class="text-sm"><code>// 接收一个输入，产生一个输出
public interface Function&lt;F, T&gt; {
    @Nullable
    T apply(@Nullable F input);
    
    @Override
    boolean equals(@Nullable Object object);
}

// 接收一个输入，产生一个布尔值
public interface Predicate&lt;T&gt; {
    boolean apply(@Nullable T input);
    
    @Override
    boolean equals(@Nullable Object object);
}

// 不接收输入，但提供一个值
public interface Supplier&lt;T&gt; {
    T get();
}

// 接收一个输入，不返回结果
public interface Consumer&lt;T&gt; {
    void accept(T input);
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-cogs text-purple-500 mr-3"></i>
                            设计特点
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>高度封装：每种功能封装为独立静态内部类</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>确保不可变性：所有实现类都是不可变的，线程安全</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>组合模式：通过compose方法支持函数组合</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>懒惰计算：函数只有在apply被调用时才执行</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>

            <div class="highlight-box">
                <h3 class="text-xl font-semibold mb-4 flex items-center text-blue-600">
                    <i class="fas fa-lightbulb mr-3"></i>
                    Functions工具类分析
                </h3>
                <div class="code-block mb-4">
                    <pre class="text-sm"><code>// Functions.java (简化版)
public final class Functions {
    private Functions() {}
    
    // 返回常量值的Function
    public static &lt;E&gt; Function&lt;Object, E&gt; constant(@Nullable final E value) {
        return new ConstantFunction&lt;E&gt;(value);
    }
    
    private static class ConstantFunction&lt;E&gt; implements Function&lt;Object, E&gt;, Serializable {
        private final E value;
        
        public ConstantFunction(@Nullable E value) {
            this.value = value;
        }
        
        @Override
        public E apply(@Nullable Object from) {
            return value;
        }
    }
    
    // 函数组合，f(g(x))
    public static &lt;A, B, C&gt; Function&lt;A, C&gt; compose(
            Function&lt;B, C&gt; f, Function&lt;A, ? extends B&gt; g) {
        return new FunctionComposition&lt;A, B, C&gt;(f, g);
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Section Divider -->
        <div class="section-divider"></div>

        <!-- Section 2 -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="bg-blue-100 text-blue-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">2</span>
                集合转换与函数应用机制
            </h2>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-list-alt text-green-500 mr-3"></i>
                            集合转换功能实现
                        </h3>
                        <div class="code-block">
                            <pre class="text-sm"><code>// Lists.java (简化版)
public static &lt;F, T&gt; List&lt;T&gt; transform(
        List&lt;F&gt; fromList, Function&lt;? super F, ? extends T&gt; function) {
    return (fromList instanceof RandomAccess)
        ? new TransformingRandomAccessList&lt;F, T&gt;(fromList, function)
        : new TransformingSequentialList&lt;F, T&gt;(fromList, function);
}

private static class TransformingRandomAccessList&lt;F, T&gt;
        extends AbstractList&lt;T&gt; implements RandomAccess, Serializable {
    final List&lt;F&gt; fromList;
    final Function&lt;? super F, ? extends T&gt; function;
    
    @Override
    public T get(int index) {
        return function.apply(fromList.get(index));
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-project-diagram text-orange-500 mr-3"></i>
                            Iterables与函数式操作
                        </h3>
                        <div class="code-block">
                            <pre class="text-sm"><code>// Iterables.java (部分)
public static &lt;F, T&gt; Iterable&lt;T&gt; transform(
        final Iterable&lt;F&gt; fromIterable,
        final Function&lt;? super F, ? extends T&gt; function) {
    checkNotNull(fromIterable);
    checkNotNull(function);
    return new FluentIterable&lt;T&gt;() {
        @Override
        public Iterator&lt;T&gt; iterator() {
            return Iterators.transform(fromIterable.iterator(), function);
        }
    };
}

public static &lt;T&gt; Iterable&lt;T&gt; filter(
        final Iterable&lt;T&gt; unfiltered, 
        final Predicate&lt;? super T&gt; predicate) {
    checkNotNull(unfiltered);
    checkNotNull(predicate);
    return new FluentIterable&lt;T&gt;() {
        @Override
        public Iterator&lt;T&gt; iterator() {
            return Iterators.filter(unfiltered.iterator(), predicate);
        }
    };
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-blue-50 rounded-xl p-6 mb-8">
                <h3 class="text-xl font-semibold mb-4 text-blue-700">关键特点</h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-eye mr-2 text-blue-600"></i>
                            视图模式
                        </h4>
                        <p class="text-gray-700">转换后的集合是原集合的视图，不会复制原始数据</p>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-tachometer-alt mr-2 text-blue-600"></i>
                            懒计算
                        </h4>
                        <p class="text-gray-700">元素只在被访问时才进行转换，而不是提前计算</p>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-bolt mr-2 text-blue-600"></i>
                            性能优化
                        </h4>
                        <p class="text-gray-700">根据原始列表是否支持RandomAccess选择不同的实现</p>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-lock mr-2 text-blue-600"></i>
                            不可变性
                        </h4>
                        <p class="text-gray-700">转换后的集合保持原集合的不可变性质</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section Divider -->
        <div class="section-divider"></div>

        <!-- Section 3 -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="bg-blue-100 text-blue-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">3</span>
                FluentIterable的链式API设计
            </h2>

            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8 card-hover transition-transform duration-300">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-link text-purple-500 mr-3"></i>
                        FluentIterable核心设计
                    </h3>
                    <div class="code-block">
                        <pre class="text-sm"><code>// FluentIterable.java (简化版)
public abstract class FluentIterable&lt;E&gt; implements Iterable&lt;E&gt; {
    
    public static &lt;E&gt; FluentIterable&lt;E&gt; from(final Iterable&lt;E&gt; iterable) {
        return (iterable instanceof FluentIterable)
            ? (FluentIterable&lt;E&gt;) iterable
            : new FluentIterable&lt;E&gt;() {
                @Override
                public Iterator&lt;E&gt; iterator() {
                    return iterable.iterator();
                }
            };
    }
    
    public &lt;T&gt; FluentIterable&lt;T&gt; transform(Function&lt;? super E, T&gt; function) {
        return from(Iterables.transform(this, function));
    }
    
    public FluentIterable&lt;E&gt; filter(Predicate&lt;? super E&gt; predicate) {
        return from(Iterables.filter(this, predicate));
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div>
                    <h3 class="text-xl font-semibold mb-4">设计亮点</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-cube text-blue-500 mt-1 mr-2"></i>
                            <span>建造者模式：每个转换方法返回一个新的FluentIterable实例</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-link text-blue-500 mt-1 mr-2"></i>
                            <span>方法链：支持method chaining，使代码更加流畅</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-hourglass-half text-blue-500 mt-1 mr-2"></i>
                            <span>延迟计算：所有中间操作都是惰性的，只有终端操作才会触发计算</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lock text-blue-500 mt-1 mr-2"></i>
                            <span>不可变结果：终端操作通常返回不可变集合，确保线程安全</span>
                        </li>
                    </ul>
                </div>
                <div>
                    <h3 class="text-xl font-semibold mb-4">实战案例</h3>
                    <div class="code-block">
                        <pre class="text-sm"><code>// 传统方式 vs Guava函数式API
List&lt;Product&gt; result = FluentIterable.from(orders)
    .filter(order -> order.getStatus() == COMPLETED 
        && order.getAmount() > 1000)
    .transformAndConcat(order -> order.getItems())
    .filter(item -> item.getCategory().equals("Electronics"))
    .transform(OrderItem::getProduct)
    .filter(product -> product.getInventory() > 0)
    .toList();</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Section Divider -->
        <div class="section-divider"></div>

        <!-- Section 4 -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-6 flex items-center">
                <span class="bg-blue-100 text-blue-800 rounded-full w-10 h-10 flex items-center justify-center mr-4">4</span>
                高级技巧：函数组合与部分应用
            </h2>

            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-puzzle-piece text-red-500 mr-3"></i>
                            函数组合实现分析
                        </h3>
                        <div class="code-block">
                            <pre class="text-sm"><code>// Functions.java - compose方法的实现
public static &lt;A, B, C&gt; Function&lt;A, C&gt; compose(
        Function&lt;B, C&gt; f, Function&lt;A, ? extends B&gt; g) {
    return new FunctionComposition&lt;A, B, C&gt;(f, g);
}

private static class FunctionComposition&lt;A, B, C&gt; 
        implements Function&lt;A, C&gt;, Serializable {
    private final Function&lt;B, C&gt; f;
    private final Function&lt;A, ? extends B&gt; g;
    
    @Override
    public C apply(@Nullable A a) {
        return f.apply(g.apply(a));
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                    <div class="p-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-cut text-orange-500 mr-3"></i>
                            部分应用与柯里化
                        </h3>
                        <div class="code-block">
                            <pre class="text-sm"><code>// 手动实现部分应用
public static &lt;A, B, C&gt; Function&lt;B, C&gt; partial(
        final Function&lt;Pair&lt;A, B&gt;, C&gt; function, final A a) {
    return new Function&lt;B, C&gt;() {
        @Override
        public C apply(B b) {
            return function.apply(Pair.of(a, b));
        }
    };
}

// 使用例子
Function&lt;Integer, Double&gt; convertFromUSD = 
    partial(convertCurrency, "USD");
double result = convertFromUSD.apply(100); // 结果: 680.0</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Mermaid Diagram -->
        <div class="bg-white rounded-xl shadow-md p-6 mb-16">
            <h3 class="text-xl font-semibold mb-4 flex items-center">
                <i class="fas fa-project-diagram text-green-500 mr-3"></i>
                Guava函数式架构图
            </h3>
            <div class="mermaid">
                graph TD
                    A[核心接口] --> B[Function]
                    A --> C[Predicate]
                    A --> D[Supplier]
                    A --> E[Consumer]
                    
                    B --> F[Functions工具类]
                    C --> G[Predicates工具类]
                    
                    F --> H[compose]
                    F --> I[constant]
                    F --> J[forMap]
                    
                    G --> K[and]
                    G --> L[or]
                    G --> M[not]
                    
                    N[集合操作] --> O[Iterables]
                    N --> P[Lists]
                    N --> Q[Maps]
                    
                    O --> R[transform]
                    O --> S[filter]
                    O --> T[find]
                    
                    P --> U[transform]
                    P --> V[partition]
                    
                    W[高级特性] --> X[FluentIterable]
                    W --> Y[函数组合]
                    W --> Z[部分应用]
            </div>
        </div>

        <!-- Section 5 & 6 combined -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-tachometer-alt text-blue-500 mr-3"></i>
                        性能优化与内存效率
                    </h3>
                    <div class="code-block mb-4">
                        <pre class="text-sm"><code>// Iterables.transform的实现
public static &lt;F, T&gt; Iterable&lt;T&gt; transform(
        final Iterable&lt;F&gt; fromIterable,
        final Function&lt;? super F, ? extends T&gt; function) {
    return new FluentIterable&lt;T&gt;() {
        @Override
        public Iterator&lt;T&gt; iterator() {
            return Iterators.transform(
                fromIterable.iterator(), 
                function
            );
        }
    };
}</code></pre>
                    </div>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>避免中间集合创建</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>按需计算元素</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                            <span>大集合处理时节省内存</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover transition-transform duration-300">
                <div class="p-6">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        最佳实践
                    </h3>
                    <div class="space-y-4">
                        <div>
                            <h4 class="font-medium mb-1">避免过度链式调用</h4>
                            <div class="code-block">
                                <pre class="text-xs"><code>// 推荐：拆分成有意义的中间变量
FluentIterable&lt;T&gt; filtered = FluentIterable.from(data).filter(...);
List&lt;U&gt; result = filtered.transform(...).toList();</code></pre>
                            </div>
                        </div>
                        <div>
                            <h4 class="font-medium mb-1">注意函数副作用</h4>
                            <div class="code-block">
                                <pre class="text-xs"><code>// 副作用单独处理
List&lt;User&gt; activeUsers = ...filter(User::isActive).toList();
for (User user : activeUsers) {
    user.setLastChecked(new Date());
}</code></pre>
                            </div>
                        </div>
                        <div>
                            <h4 class="font-medium mb-1">明智选择终端操作</h4>
                            <div class="code-block">
                                <pre class="text-xs"><code>// 需要List：使用toList()
// 需要不可变集合：使用toImmutableList()
// 需要Map：使用toMap()</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Final Thoughts -->
        <div class="bg-indigo-50 rounded-xl p-8 mb-12">
            <h3 class="text-2xl font-bold text-center mb-6 text-indigo-800">总结与思考</h3>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-lightbulb text-3xl"></i>
                    </div>
                    <h4 class="font-semibold mb-2">设计思想</h4>
                    <p class="text-gray-700">Guava展示了如何在Java中实现优雅的函数式编程，即使在Java 8之前也能提供强大的功能。</p>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-code-branch text-3xl"></i>
                    </div>
                    <h4 class="font-semibold mb-2">实现技巧</h4>
                    <p class="text-gray-700">通过视图模式、延迟计算和不可变性等设计，实现了高效且安全的功能。</p>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-graduation-cap text-3xl"></i>
                    </div>
                    <h4 class="font-semibold mb-2">学习价值</h4>
                    <p class="text-gray-700">这些设计模式和实现技巧对理解现代Java库和框架的实现原理非常有帮助。</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto px-4 text-center">
            <div class="mb-4">
                <h3 class="text-xl font-semibold">技术小馆</h3>
            </div>
            <div class="mb-4">
                <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-300">
                    <i class="fas fa-link mr-2"></i>
                    http://www.yuque.com/jtostring
                </a>
            </div>
            <div class="text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>